package org.example;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ExcelReader {

    public static void main(String[] args) {
        String filePath = "C:\\Users\\lvenx\\Downloads\\期间核查导入导出模板.xlsx"; // 替换为你的文件路径

        try (FileInputStream fis = new FileInputStream(new File(filePath));
             XSSFWorkbook workbook = new XSSFWorkbook(fis)) {

            XSSFSheet sheet = workbook.getSheetAt(0); // 获取第一个 sheet
            int lastRowNum = sheet.getLastRowNum();   // 获取最后一行行号

            // 遍历行
            for (int i = 6; i <= lastRowNum; i++) {  // 从第6行开始（跳过标题）
                Row row = sheet.getRow(i);
                if (row == null) continue; // 跳过空行
                IntervalCheck intervalCheck = new IntervalCheck();
                // 读取列数据
                String systemID = getCellValueAsString(row.getCell(1));     // 设备系统ID
                String deviceName = getCellValueAsString(row.getCell(2));         // 设备名称
                String deviceCode = getCellValueAsString(row.getCell(3));     // 设备编号
                String OneMonth = getCellValueAsString(row.getCell(4));     // 设备编号

                intervalCheck.setEquipmentId(Long.parseLong(systemID));
//                intervalCheck.set
                // 读取 1-12 月数据
                StringBuilder monthlyData = new StringBuilder();
                for (int j = 4; j <= 15; j++) { // 从第5列到第16列
                    monthlyData.append(getCellValueAsString(row.getCell(j))).append(",");
                }


            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 工具方法：获取单元格值并转换为字符串
     *
     * @param cell 单元格
     * @return 字符串形式的单元格值
     */
    private static String getCellValueAsString(Cell cell) {
        if (cell == null) {
            return ""; // 空单元格返回空字符串
        }
        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue();
            case NUMERIC:
                // 判断是否是整数
                if (cell.getNumericCellValue() % 1 == 0) {
                    return String.valueOf((int) cell.getNumericCellValue());
                } else {
                    return String.valueOf(cell.getNumericCellValue());
                }
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            case FORMULA:
                return cell.getCellFormula();
            default:
                return "";
        }
    }

}
